package leetcode_top;
import java.util.*;

public class Ex36 {
    class Solution {
        public boolean isValidSudoku(char[][] board) {
            if (board == null || board[0] == null) return false;
            Set<Character> set = new HashSet<>();
            //检查行
            for (int i = 0;i < 9; i++) {
                set.clear();
                for (int j = 0; j < 9; j++) {
                    if (set.contains(board[i][j])) return false;
                    set.add(board[i][j]);
                }
            }
            //检查列
            for (int i = 0;i < 9; i++) {
                set.clear();
                for (int j = 0; j < 9; j++) {
                    if (set.contains(board[j][i])) return false;
                    set.add(board[j][i]);
                }
            }
            //检查网格
            int cow = 0, row = 0;
            while (cow < 9 && row < 9) {
                set.clear();
                for (int i = cow; i < cow + 3; i++) {
                    for (int j = row; j < row + 3; j++) {
                        if (set.contains(board[i][j])) return false;
                        set.add(board[i][j]);
                    }
                }
                row += 3;
                if (row == 9) {
                    row = 0;
                    cow += 3;
                }
            }
            return true;
        }
    
        public boolean isDigit(char c) {
            return c >= '0' && c <= '9';
        }
    }
}
